package com.wz.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PacketUtils {

	/** 字符 '0' */
	private static final char OCT_FIX_SPACE = 0x30;
	/** 空格字符' ' */
	private static final char STRING_FIX_SPACE = 0x20;

	/**
	 * 取当前时间ddHHmmss + 2位随机数，生成报文序列号，一定时间内不重复
	 * 
	 * @return 报文序列号
	 * @since 1.0
	 */
	public synchronized static String getSN() {
		return DateUtils.getNowTime("ddHHmmss")
				+ RandomStringUtils.randomNumeric(2);
	}

	/**
	 * 移除Oct类字符串左补的字符 {@link PacketUtils#OCT_FIX_SPACE}
	 * 
	 * @param orginal
	 * @return
	 * @since 1.0
	 */
	public static int unFixOct(String orginal) {
		/*
		 * if (orginal == null) return 0; return Integer.parseInt(orginal);
		 */
		if (orginal == null)
			return 0;
		int index = 0;
		for (; (OCT_FIX_SPACE == orginal.charAt(index)); index++)
			;
		orginal = orginal.substring(index);
		return Integer.parseInt(orginal);
	}

	/**
	 * Oct类字符串长度不足位左补字符 {@link PacketUtils#OCT_FIX_SPACE}
	 * 
	 * @param orginal
	 * @param len
	 * @since 1.0
	 * @return 左补字符 {@link PacketUtils#OCT_FIX_SPACE}后的字符串
	 */
	public static String fixOct(String orginal, int len) {
		if (orginal == null)
			orginal = "";
		if (orginal.length() < len) {
			while (orginal.length() < len) {
				orginal = OCT_FIX_SPACE + orginal;
			}
		} else if (orginal.length() > len) {
			throw new IllegalArgumentException();
		}
		return orginal;
	}

	/**
	 * String类型字符串长度不足位左补空格 {@link PacketUtils#STRING_FIX_SPACE}
	 * 
	 * @param orginal
	 * @param len
	 * @since 1.0
	 * @return 左补空格 {@link PacketUtils#STRING_FIX_SPACE}后的字符串
	 */
	public static String fixString(String orginal, int len) {
		if (orginal == null)
			orginal = "";
		if (orginal.getBytes().length < len) {
			while (orginal.getBytes().length < len) {
				orginal = orginal + STRING_FIX_SPACE;
			}
		} else if (orginal.getBytes().length > len) {
			throw new IllegalArgumentException();
		}
		return orginal;
	}

	public static String fixOct(int orginal, int len) {
		return fixOct(orginal + "", len);
	}

	public static int getChineseNum(String str) {
		int count = 0;
		String regEx = "[\\u4e00-\\u9fa5]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		while (m.find())
			count++;
		return count;
	}

	/**
	 * 判断长度是否合法
	 * 
	 * @param data
	 * @param length
	 * @return
	 */
	public static String isLegal(String data, int length)
			throws IllegalArgumentException {
		if (data == null)
			data = "NA";
		if (data.getBytes().length > length) {
			throw new IllegalArgumentException();
		}
		return data;
	}

	/**
	 * 日期转化
	 * @param date 要转化的时间
	 * @param useFormat 目前的日期格式
	 * @param tagertFormat 目标日期格式
	 * @return 目标时间
	 * @author wz
	 * @throws ParseException 
	 */
	public static String getNewDate(String date, String useFormat,String tagertFormat) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(useFormat);
		Date day;
		day = sdf.parse(date);
		sdf = new SimpleDateFormat(tagertFormat);
		return sdf.format(day);
	}
}
